package com.banner.lyl.utils;

import java.sql.*;


/**
 * @author lvyuanlong
 */
public class JavaDateConnection {
    /**
     * 获取数据库连接
     * @return Connection
     */
    public  Connection getConn() {
        String url = "jdbc:mysql://127.0.0.1:3306/taobao";
        String username = "root";
        String password = "root";
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //classLoader,加载对应驱动
            conn = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }


    /**
     * 关闭Connection PreparedStatement
     * @param connection
     * @param preparedStatement
     */
    public static void closeConnection(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
